﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Text.Json;
using Sunny.UI.Win32;
using Traceability.Commont.Domains.AnimalsDomainFiles;

namespace Butcher.winform.EnterpriseForms.SlaughteredDetailnes
{
    public partial class SlaughterCreate : Form
    {
        public SlaughterCreate()
        {
            InitializeComponent();
            getApp();
            getShop();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            string url = @"http://localhost:5229/api/Batch/AddSlaughterCraete";
            HttpClient client = new HttpClient();
            SlaughteredDetails ct = new SlaughteredDetails();
            ct.ApproachId = Convert.ToInt32(uiComboBox2.SelectedValue);
            ct.Suppliers = uiTextBox1.Text;
            ct.SlaughterCircleNum = Convert.ToInt32(uiTextBox2.Text);
            ct.Weight = Convert.ToDecimal(uiTextBox3.Text);
            ct.ShoppingDesignationid = Convert.ToInt32(uiComboBox1.SelectedValue);
            ct.ApproachBatch = uiTextBox4.Text;
            ct.AverageWeight = Convert.ToDecimal(uiTextBox5.Text);
            ct.Remark = uiTextBox6.Text;
            ct.ApproachNo = "";
            ct.SlaughterCircleId = 0;
            ct.WeighTime = DateTime.Now;
            ct.Status = 0;
            
            var content = new StringContent(JsonSerializer.Serialize<SlaughteredDetails>(ct));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            saaa = info["messges"].Value<string>();
            if (saaa != null)
            {
                MessageBox.Show("新增成功");
                //SearchClient departmentview = new SearchClient();
                //this.Hide();
                //departmentview.ShowDialog();
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }
        public void getShop()
        {
            Shopping shopping = new Shopping();
            string url = @"http://localhost:5229/api/Batch/getShop";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Shopping>(shopping));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Shopping> enterprises = aaa.ToObject<List<Shopping>>();
            this.uiComboBox1.DataSource = enterprises;
            this.uiComboBox1.DisplayMember = "ShoppingDesignationName";
            this.uiComboBox1.ValueMember = "ShoppingDesignationid";
        }

        private void getApp()
        {
            Approach approach = new Approach();
            string url = @"http://localhost:5229/api/Enterprise/Searchapp";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Approach>(approach));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Approach> enterprises = aaa.ToObject<List<Approach>>();
            this.uiComboBox2.DataSource = enterprises;
            this.uiComboBox2.DisplayMember = "ApproachNo";
            this.uiComboBox2.ValueMember = "ApproachId";
        }
    }
}
